package com.example.labSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.labSystem.entity.Leave;
import com.example.labSystem.mapper.LeaveMapper;
import com.example.labSystem.service.ILeaveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 请假申请表 服务实现类
 * </p>
 *
 * @author 10
 * @since 2024-12-12
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements ILeaveService {

    /**
     * 查询所有请假记录，结果缓存到 Redis
     * 缓存的 key 是 "allLeaves"
     */
    @Override
    @Cacheable(value = "leaveCache", key = "'allLeaves'", unless = "#result == null")
    public List<Leave> getAllLeaves() {
        return this.list();
    }

    /**
     * 根据用户名查询请假记录，假设用户名唯一，结果缓存到 Redis
     * 缓存的 key 是 "userID_{userID}"
     */
    @Override
    @Cacheable(value = "leaveCache", key = "'userID_' + #userID", unless = "#result == null")
    public List<Leave> getLeavesByUserId(String userID) {
        QueryWrapper<Leave> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userID", userID);
        return this.list(queryWrapper);
    }

    /**
     * 根据请假申请ID获取请假记录
     *
     * @param requestID 请假申请ID
     * @return 请假记录
     */
    @Override
    @Cacheable(value = "leaveCache", key = "#requestID", unless = "#result == null")
    public Leave getLeaveByRequestID(String requestID) {
        return this.getById(requestID);
    }

    /**
     * 更新请假记录，并清除缓存
     *
     * @param leave 请假记录
     * @return 更新成功标志
     */
    @Override
    @CacheEvict(value = "leaveCache", allEntries = true)
    public boolean updateLeave(Leave leave) {
        return this.updateById(leave);
    }

    /**
     * 审核请假记录
     * @param requestID 请假记录ID
     * @param status 审核状态（approved/rejected）
     * @return 审核是否成功
     */
    @Override
    @CacheEvict(value = "leaveCache", key = "'allLeaves'")
    @CachePut(value = "leaveCache", key = "#requestID")
    public boolean approveLeave(String requestID, String status) {
        Leave leave = this.getById(requestID);
        if (leave != null && "pending".equals(leave.getStatus())) {
            leave.setStatus(status);
            // 更新缓存
            this.updateById(leave);
            return true;
        }
        return false;
    }

    /**
     * 删除请假记录，并清除缓存
     *
     * @param requestID 请假申请ID
     * @return 删除成功标志
     */
    @Override
    @CacheEvict(value = "leaveCache", allEntries = true)
    public boolean deleteLeave(String requestID) {
        return this.removeById(requestID);
    }

    /**
     * 提交新的请假申请
     *
     * @param leave 请假记录
     * @return 提交成功标志
     */
    @Override
    @CacheEvict(value = "leaveCache", allEntries = true)
    public boolean submitLeaveRequest(Leave leave) {
        return this.save(leave);
    }
}
